﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TicketingCompany.DAL
{
    public static class TicketingSystemDAO
    {
        public static void AddCategory(Category newCategory)
        {
            TicketingCompaniesModel ticketingCompany = new TicketingCompaniesModel();
            ticketingCompany.Add(newCategory);
            ticketingCompany.SaveChanges();
        }

        public static void AddEvent(Event newEvent)
        {
            TicketingCompaniesModel ticketingCompany = new TicketingCompaniesModel();
            ticketingCompany.Add(newEvent);
            ticketingCompany.SaveChanges();
        }

        public static void MakePurchase(Purchase newPurchase)
        {
            TicketingCompaniesModel ticketingCompany = new TicketingCompaniesModel();
            ticketingCompany.Add(newPurchase);
            ticketingCompany.SaveChanges();
        }

        public static void AddLocation(Location newLocation)
        {
            TicketingCompaniesModel ticketingCompany = new TicketingCompaniesModel();
            ticketingCompany.Add(newLocation);
            ticketingCompany.SaveChanges();
        }

        public static Location GetLocation(int locationID, TicketingCompaniesModel ticketingCompany)
        {
             return ticketingCompany.Locations.FirstOrDefault(l => l.LocationID == locationID);
        }

        public static Category GetCategory(int categoryID, TicketingCompaniesModel ticketingCompany)
        {
            return ticketingCompany.Categories.FirstOrDefault(c =>c.CategoryID == categoryID);
        }

        public static Event GetEvent(int eventID, TicketingCompaniesModel ticketingCompany)
        {
            return ticketingCompany.Events.FirstOrDefault(e => e.EventID == eventID);
        }

        public static Purchase GetPurchase(int purchaseID, TicketingCompaniesModel ticketingCompany)
        {
            return ticketingCompany.Purchases.FirstOrDefault(p => p.PurchaseID == purchaseID);
        }

        public static bool UpdateLocation(int locationID, Location location)
        {
            TicketingCompaniesModel ticketingCompnayEntities = new TicketingCompaniesModel();
            try
            {
                Location locationFound = GetLocation(locationID, ticketingCompnayEntities);
                locationFound = location;
                ticketingCompnayEntities.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool UpdateCategory(int categoryID, Category category)
        {
            TicketingCompaniesModel ticketingCompnayEntities = new TicketingCompaniesModel();
            try
            {
                Category categoryFound = GetCategory( categoryID, ticketingCompnayEntities);
                categoryFound = category;
                ticketingCompnayEntities.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool UpdateEvent(int eventID, Event eventUpdated)
        {
            TicketingCompaniesModel ticketingCompnayEntities = new TicketingCompaniesModel();
            try
            {
                Event eventFound = GetEvent(eventID, ticketingCompnayEntities);
                eventFound = eventUpdated;
                ticketingCompnayEntities.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static Event[] FindUpcomingEvents()
        {
            TicketingCompaniesModel TicketingCompaniesModel = new TicketingCompaniesModel();
            var upcomingEvents = TicketingCompaniesModel.Events.Where(e => e.Date > DateTime.Now.AddDays(3)).
                OrderBy(e => e.Date);

            return upcomingEvents.ToArray();
        }

        public static Purchase[] GetPurcaseByStatus(bool isDelevered) 
        {
            TicketingCompaniesModel TicketingCompaniesModel = new TicketingCompaniesModel();
            var purcheses = TicketingCompaniesModel.Purchases.Where
                (p => p.IsDelivered == isDelevered).OrderBy(p=>p.Date);
            return purcheses.ToArray();
        }

        public static void ChangeStatusOfPurchase(int purchaseID)
        {
            TicketingCompaniesModel entities = new TicketingCompaniesModel();
            Purchase purchase = GetPurchase(purchaseID, entities);
            purchase.IsDelivered = true;
            entities.SaveChanges();
        }

    }
}
